<template>
  <!-- 首页配置配置 -->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="序号" prop="serial">
        <el-input
          v-model="queryParams.serial"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="首页配置标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入首页配置标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片" prop="pic">
        <el-input
          v-model="queryParams.pic"
          placeholder="请输入图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="链接" prop="outerInterlinkage">
        <el-input
          v-model="queryParams.outerInterlinkage"
          placeholder="请输入链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="位置" prop="location">
        <el-select v-model="queryParams.location" placeholder="请选择">
          <el-option label="轮播图" value="1"></el-option>
          <el-option label="首页按钮" value="2"></el-option>
          <el-option label="广告位" value="3"></el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.location"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <!-- <el-form-item label="首页配置类别编号" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入首页配置类别编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="首页配置作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入首页配置作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['committee:vxFirstPage:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['committee:vxFirstPage:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['committee:vxFirstPage:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['committee:vxFirstPage:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="journalismList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="60" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="serial" />
      <el-table-column label="${comment}" align="center" prop="id" /> -->
      <!-- <el-table-column label="首页配置标题" align="center" prop="title" /> -->
      <el-table-column label="位置" align="center" prop="location">
        <template slot-scope="scope">
          {{ scope.row.location == 1 ? '轮播图' : scope.row.location == 2 ? '首页按钮' : '广告位' }}
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="pic">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.pic"
            style="width: 100px; height: 100px"
            :src="scope.row.pic"
            :preview-src-list="[scope.row.pic]"
            fit="cover"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="链接" align="center" prop="outerInterlinkage" />
      <!-- <el-table-column label="内容" align="center" prop="substance" /> -->
      <!-- <el-table-column label="首页配置类别编号" align="center" prop="categoryId" />
      <el-table-column label="首页配置作者" align="center" prop="author" /> -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['committee:vxFirstPage:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['committee:vxFirstPage:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改首页配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="50px">
        <!-- <el-form-item label="序号" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入序号" />
        </el-form-item> -->
        <el-form-item label="位置" prop="location">
          <!-- <el-input v-model="form.location" placeholder="请输入位置" /> -->
          <el-select style="width: 35%" v-model="form.location" placeholder="请选择">
            <el-option label="轮播图" value="1"></el-option>
            <el-option label="首页按钮" value="2"></el-option>
            <el-option label="广告位" value="3"></el-option>
          </el-select>
          <span v-if="form.location == 2" style="color: #999">
            按钮最多显示6个，多出将被隐藏！</span
          >
          <span v-if="form.location == 3" style="color: #999"> 广告位只有1个，取排序第一个！</span>
        </el-form-item>
        <el-form-item v-if="form.location == 2" label="标题" prop="title">
          <el-input style="width: 50%" v-model="form.title" placeholder="请输入标题" />
          <!-- <span v-if="form.location==1" style="color: #999"> 轮播图推荐分辨率690*388，等比例放大缩小</span> -->
        </el-form-item>
        <el-form-item label="图片" prop="pic">
          <el-upload
            style="width: 100%"
            :action="uploadFileUrl"
            :limit="1"
            list-type="picture"
            :on-change="changePicImage"
            :on-remove="removePicImage"
            :on-error="handleUploadError"
            :on-success="handleUploadSuccess"
            :file-list="picFileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <span v-if="form.location == 1" style="color: #999">
              轮播图推荐分辨率690*388，等比例放大缩小</span
            >
            <span v-if="form.location == 2" style="color: #999">
              按钮图标推荐宽高比1：1，等比例放大缩小</span
            >
            <span v-if="form.location == 3" style="color: #999">
              广告位推荐分辨率676*169，等比例放大缩小</span
            >
          </el-upload>
        </el-form-item>
        <!-- <el-form-item label="首页配置标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入首页配置标题" />
        </el-form-item> -->
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="链接" prop="outerInterlinkage">
          <el-input v-model="form.outerInterlinkage" placeholder="请输入链接" />
        </el-form-item>
        <!-- <el-form-item label="内容" prop="substance">
          <el-input v-model="form.substance" type="textarea" :autosize="{ minRows: 4, maxRows: 8}" placeholder="请输入内容" />
        </el-form-item> -->
        <!-- <el-form-item label="首页配置类别编号" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入首页配置类别编号" />
        </el-form-item>
        <el-form-item label="首页配置作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入首页配置作者" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJournalism,
  getJournalism,
  delJournalism,
  addJournalism,
  updateJournalism,
} from '@/api/journalism';
import { GetUrlString } from '@/utils/string';

export default {
  name: 'Journalism',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 首页配置表格数据
      journalismList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 图片
      picFileList: [],
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/api/images/uploadLocal',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: 2,
        serial: null,
        title: null,
        sort: null,
        pic: null,
        outerInterlinkage: null,
        substance: null,
        location: null,
        author: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        location: [{ required: true, message: '位置不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '按钮标题不能为空', trigger: 'blur' }],
        pic: [{ required: true, message: '图片不能为空', trigger: 'blur' }],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        // outerInterlinkage: [{ required: true, message: '链接不能为空', trigger: 'blur' }],
        // substance: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询首页配置列表 */
    getList() {
      this.loading = true;
      listJournalism(this.queryParams).then(response => {
        this.journalismList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serial: null,
        id: null,
        title: null,
        sort: null,
        pic: null,
        outerInterlinkage: null,
        substance: null,
        location: null,
        author: null,
        createTime: null,
      };
      this.picFileList = [];
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加首页配置';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getJournalism(id).then(response => {
        this.form = response.data;
        this.picFileList = this.form.pic? [{ name: GetUrlString(this.form.pic, 'name'), url: this.form.pic }]: [];
        this.open = true;
        this.title = '修改首页配置';
      });
    },

    changePicImage(file, fileList) {
      console.log(file.code);
      if (file.status === 'success') {
        this.form.pic = file.response.data.url;
        this.picFileList = fileList;
      }
    },
    removePicImage() {
      this.form.pic = null;
      this.picFileList = [];
    },
    handleUploadError() {
      this.$modal.msgError('上传失败');
    },
    handleUploadSuccess(res) {
      console.log(res, 'resssss');
      if (res.code == 200) {
        this.$modal.msgSuccess('上传成功');
      } else {
        this.$modal.msgError('上传失败');
      }
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.pic.indexOf("?") != -1){
        var index = this.form.pic.indexOf("=");
        this.form.pic = this.form.pic.substr(index + 1,this.form.pic.length);
      }
      this.form.categoryId = 2;
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJournalism(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addJournalism(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除首页配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delJournalism(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'committee/journalism/export',
        {
          ...this.queryParams,
        },
        `journalism_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
